package service

import (
	"context"
	"user-service/basic/config"
	__ "user-service/basic/proto"
	"user-service/handler/model"
)

func (s *Server) HospitalList(_ context.Context, in *__.HospitalListReq) (*__.HospitalListResp, error) {
	page := in.Page
	if page <= 0 {
		page = 1
	}

	pageSize := in.PageSize
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	var hospital []model.Hospital
	config.DB.Offset(int(offset)).Limit(int(pageSize)).Find(&hospital)
	var hospitallist []*__.HospitalList
	for _, m := range hospital {
		list := __.HospitalList{
			HospitalName: m.HospitalName,
			Level:        m.Level,
			Province:     m.Province,
			City:         m.City,
			Address:      m.Address,
			Phone:        m.Phone,
			Introduction: m.Introduction,
		}
		hospitallist = append(hospitallist, &list)
	}
	return &__.HospitalListResp{
		List: hospitallist,
	}, nil
}
